﻿//功能描述：客户管理录入数据访问层
using Hong.Common.Encryption;
using Hong.IRepository.IRepository_CustomerManagement;
using Hong.Model.Dto.Model_CustomerManagementDto;
using Hong.Model.SysDomain.Model_CustomerManagement;
using Hong.Model.SysDomain.Model_SystemSettings;
using Hong.Repository.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hong.Repository.Repository.Repository_CustomerManagement
{
    public class CustInfoRepository: ICustInfoRepository
    {
        #region 构造器注入
        private readonly T_HongShi_3Context _dal;
        public CustInfoRepository(T_HongShi_3Context dal, SecurityStatic _securityStatic)
        {
            _dal = dal;
        }
        #endregion

        #region 客户信息录入
        #region 甲方负责人
        /// <summary>
        /// 删除甲方负责人
        /// </summary>
        /// <returns></returns>
        public int DeletePerson(string id)
        {
            var list = _dal.People.Find(id);
            _dal.People.Remove(list);
            return _dal.SaveChanges();
        }



        /// <summary>
        /// 显示甲方负责人
        /// </summary>
        /// <returns></returns>
        public List<Person> GetPerson(string custid)
        {
            var list = _dal.People.ToList();
            if (!string.IsNullOrEmpty(custid))
            {
                list = list.Where(x => x.CustomerId == custid).ToList();
            }

            return list;
        }
        /// <summary>
        /// 反填甲方负责人
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Person GetPersonFT(string id)
        {
            var list = _dal.People.FirstOrDefault(x => x.PersonId == id);
            return list;
        }
        #endregion

        #region 添加基本信息
        /// <summary>
        /// 添加客户基本信息
        /// </summary>
        /// <returns></returns>
        public int PostCustomer(Customer customer)
        {
            try
            {
                var i = _dal.Customers.Add(customer);
                return _dal.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 添加甲方负责人信息
        /// </summary>
        /// <param name="person"></param>
        /// <returns></returns>
        public int PostPerson(Person person)
        {
            try
            {
                var i = _dal.People.Add(person);
                return _dal.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改甲方负责人
        /// </summary>
        /// <param name="person"></param>
        /// <returns></returns>
        public int PutPerson(Person person)
        {
            _dal.Entry(person).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _dal.SaveChanges();
        }
        #endregion

        #region 扩展信息
        /// <summary>
        /// 国家
        /// </summary>
        /// <returns></returns>
        public List<Address> GetAddress1()
        {
            var list = _dal.Addresses.Where(x => x.AddressToupperId == "0").ToList(); ;
            return list;
        }
        /// <summary>
        /// 省市
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Address> GetAddress2(string id)
        {
            var list = _dal.Addresses.Where(x => x.AddressToupperId == id).ToList();
            return list;
        }

        /// <summary>
        /// 客户类型
        /// </summary>
        /// <returns></returns>

        public List<Dictionary> GetTypes()
        {
            var list = _dal.Dictionaries.Where(x => x.DicCode == "cst_type").ToList();
            return list;
        }
        /// <summary>
        /// 所属行业
        /// </summary>
        /// <returns></returns>

        public List<Dictionary> GetIndustry()
        {
            var list = _dal.Dictionaries.Where(x => x.DicCode == "cst_pro").ToList();
            return list;
        }
        /// <summary>
        /// 添加扩展信息
        /// </summary>
        public int PostExtended(Extended extended)
        {
            var i = _dal.Extendeds.Add(extended);
            return _dal.SaveChanges();
        }
        #endregion

        #region 满意度调查
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="satisfaction"></param>
        /// <returns></returns>
        public int PostSatisfaction(Satisfaction satisfaction)
        {
            var i = _dal.Satisfactions.Add(satisfaction);
            return _dal.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteSatisfaction(string id)
        {
            var list = _dal.Satisfactions.Find(id);
            _dal.Satisfactions.Remove(list);
            return _dal.SaveChanges();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<Satisfaction> GetSatisfaction(string custid)
        {
            var list = _dal.Satisfactions.ToList();
            if (!string.IsNullOrEmpty(custid))
            {
                list = list.Where(x => x.CustomerId == custid).ToList();
            }
            return list;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Satisfaction GetSatisfactionFT(string id)
        {
            var list = _dal.Satisfactions.FirstOrDefault(x => x.SaId == id);
            return list;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="satisfaction"></param>
        /// <returns></returns>
        public int PutSatisfaction(Satisfaction satisfaction)
        {
            _dal.Entry(satisfaction).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _dal.SaveChanges();
        }
        #endregion

        #region 附件
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="satisfaction"></param>
        /// <returns></returns>
        public int PostAccessory(Accessory accessory)
        {
            var i = _dal.Accessories.Add(accessory);
            return _dal.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteAccessory(string id)
        {
            var list = _dal.Accessories.Find(id);
            _dal.Accessories.Remove(list);
            return _dal.SaveChanges();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<Accessory> GetAccessory(string custid)
        {
            var list = _dal.Accessories.ToList();
            if (!string.IsNullOrEmpty(custid))
            {
                list = list.Where(x => x.CustomerId == custid).ToList();
            }
            return list;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Accessory GetAccessoryFT(string id)
        {
            var list = _dal.Accessories.FirstOrDefault(x => x.AcsoryId == id);
            return list;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="satisfaction"></param>
        /// <returns></returns>
        public int PutAccessory(Accessory accessory)
        {
            _dal.Entry(accessory).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _dal.SaveChanges();
        }
        #endregion
        #endregion


    }
}
